﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LayoutTest
{
    public partial class FlowLayout : Form
    {
        private List<String> controlNameList;
        public FlowLayout()
        {
            InitializeComponent();
            controlNameList = null;
        }

        private void FlowLayout_Load(object sender, EventArgs e)
        {
            flowLayoutPanel1.AutoScroll = true;
            //flowLayoutPanel1.
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Label lbltemp = new Label();
            lbltemp.TextAlign = ContentAlignment.BottomCenter;
            lbltemp.AutoSize = true;
            lbltemp.MinimumSize = new Size(100,20);
            lbltemp.MaximumSize = new Size(300,20);
            lbltemp.Name = "lbl" + getName("T");
            lbltemp.Text = "T";
            lbltemp.Dock = DockStyle.Left;
            //this.flowLayoutPanel1.ClientSize
            

            Button btntemp = new Button();
            btntemp.AutoSize = true;
            btntemp.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
            btntemp.MinimumSize = new Size(100, 20);
            btntemp.MaximumSize = new Size(300, 20);
            btntemp.Name = "btn" + getName("T");
            btntemp.Text = "T";
            btntemp.Dock = DockStyle.Right;
            btntemp.Click += new System.EventHandler(this.button_Click);

            if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                lbltemp.Name = "lbl" + getName(textBox1.Text.Trim());
                lbltemp.Text = textBox1.Text.Trim() + ":";
                btntemp.Name = "btn" + getName(textBox1.Text.Trim());
                btntemp.Text = textBox1.Text.Trim();
            }
            addControlNameList(lbltemp.Name.Substring(3));
            TableLayoutPanel tlp1 = new TableLayoutPanel();
            
            //tlp1.Width

            tlp1.BackColor = Color.LightGray;
            tlp1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
            tlp1.AutoSize = true;
            tlp1.GrowStyle = TableLayoutPanelGrowStyle.AddColumns;
            tlp1.MinimumSize = new Size(100,30);
            tlp1.MaximumSize = new Size(500,30);
            tlp1.SetCellPosition(lbltemp, new TableLayoutPanelCellPosition(0, 0));
            tlp1.SetCellPosition(btntemp, new TableLayoutPanelCellPosition(1, 0));
            tlp1.Controls.Add(lbltemp);
            tlp1.Controls.Add(btntemp);
            tlp1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
            flowLayoutPanel1.Controls.Add(tlp1);
        }

        private void addControlNameList(String controlName)
        {
            if (controlNameList == null)
                controlNameList = new List<string>();
            controlNameList.Add(controlName);
        }

        private void printControlNameList()
        {
            if(controlNameList==null)
                return;
            String printBuff = "";
            foreach (string name in controlNameList)
            {
                printBuff += name + "\n";
            }
            if(!String.IsNullOrEmpty(printBuff))
                MessageBox.Show(printBuff);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            printControlNameList();
        }

        private string getName(String oldName)
        {
            return getName(oldName, 1);
        }

        private string getName(String oldName, int counter)
        {
            if (controlNameList == null || String.IsNullOrEmpty(oldName))
                return oldName;
            string newName = "";
            foreach (string name in controlNameList)
            {
                if (string.Equals(name, oldName))
                {
                    if (counter == 1)
                        newName = getName(oldName + "_" + counter, ++counter);
                    else if (counter == 10)
                        newName = getName(oldName.Substring(0, oldName.Length - 2) + "_" + counter, ++counter);
                    else
                        newName = getName(oldName.Substring(0, oldName.Length - 1 - counter.ToString().Length) + "_" + counter, ++counter);
                }
            }
            return String.IsNullOrEmpty(newName)?oldName:newName;
        }

        private void setAction()
        {
            if (controlNameList == null)
                return;
            foreach (string name in controlNameList)
            {
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    if (string.Equals(c.Name, "btn" + name))
                    {
                        c.Click += new System.EventHandler(this.button_Click);
                    }
                }
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hi");
        }
        
    }
}
